<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2020/1/3
 * Time: 9:51
 */
namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Http\Request\System\Role\GetPowerTreeRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Common\Utils\JsonResponse;
use App\Http\Request\System\Role\ListRequest;
use App\Http\Request\System\Role\AddRequest;
use App\Http\Request\System\Role\EditRequest;
use App\Http\Request\System\Role\DeleteRequest;
use App\Http\Request\System\Role\RolepowerRequest;
use App\Http\Request\System\Role\SetrolepowerRequest;
use Illuminate\Http\Request;

class RoleController extends Controller
{
    /** @var \App\Services\System\RoleService $roleService */
    private $roleService;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
        $this->roleService = app('roleService');
    }

    /**
     * 主页
     */
    public function index()
    {
        return view('systems.roles.index');
    }

    /**
     * 获取列表
     */
    public function getList(ListRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->roleService->getList($requestData);
                return JsonResponse::get($result['data'],TRUE,$result['total']);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 新增
     */
    public function add(AddRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->roleService->add($requestData);
                $logService = app('logService');
                $logService->log('163','新增角色', "新增角色，角色名称[{$requestData['rolename']}]");
                return JsonResponse::success($result);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 编辑
     */
    public function edit(EditRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->roleService->edit($requestData);
                $logService = app('logService');
                $logService->log('164','修改角色', "修改角色，编号[{$requestData['id']}]角色名称[{$requestData['rolename']}]");
                return JsonResponse::operate();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 删除
     */
    public function delete(DeleteRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->roleService->delete($requestData);
                $logService = app('logService');
                $logService->log('165','删除角色', "删除角色，编号[{$requestData['id']}]");
                return JsonResponse::operate();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    //获取权限树
    public function getPowerTree(GetPowerTreeRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->roleService->getPowerTree($requestData);
                return JsonResponse::get($result);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }

    }

    /**
     * 获取角色权限
     */
    public function getRolePowers(RolepowerRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->roleService->getRolePowers($requestData);
                return JsonResponse::get($result);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 设置角色权限
     */
    public function setRolePowers(SetrolepowerRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->roleService->setRolePowers($requestData);
                $logService = app('logService');
                $logService->log('166','设置角色权限', "设置角色权限，角色编号[{$requestData['id']}]");
                return JsonResponse::success();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * @Description: 获取角色人员
     * @Author: kzj
     * @datetime: 2022-11-26
     */
    public function getRoleUsers(Request $request)
    {
        try
        {
            $result = $this->roleService->getRoleUsers($request->all());
            return JsonResponse::success($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        } 
    }

}
